SA

Column

Column

Votes by Polling Places

Overall Votes

Column

NSW

Column

Column

Votes by Polling Places

Overall Votes

Column

---
title: "Australian Federal Election 2016 - Polling Place Breakdown [TENTATIVE]"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    storyboard: false
    theme: united
    social: [ "twitter" ]
    source_code: embed
    highlight: pygments
---

```{r setup, include=FALSE}
## BASED ON BREXIT VOTE FLEXDASHBOARD:
## https://github.com/p0bs/p0bs.github.io/blob/master/BrexitDashboard.Rmd
## by Robin Penfold
# library(shiny)
library(flexdashboard)
library(htmltools)
library(crosstalk)
library(rmarkdown)        # Generates this html report
library(knitr)            # Underpins this html report
library(xml2)             # Scrapes web data
library(rvest)            # Scrapes web data
library(tidyr)            # Restructures data
library(dplyr)            # Provides database functionality
library(htmlwidgets)      # Enables responsive exhibits
library(DT)               # Enables responsive data tables
library(leaflet)          # Generates interactive map plots
library(magrittr)         # Enables chaining of commands
library(maptools)
# library(ggplot2)
library(sp)
library(plotly)

# AEC <- rgdal::readOGR(dsn = ".", layer = "COM_ELB")
# saveRDS(AEC, file = "AEC.rds")
AEC <- readRDS(file = "AEC.rds")
AEC.SA  <- AEC[AEC$State == "SA", ]
AEC.NSW <- AEC[AEC$State == "NSW", ]

# polling.SA  <- read.csv("GeneralPollingPlacesDownload-20499.csv", stringsAsFactors = FALSE) %>% filter(State == "SA", !is.na(Latitude), !is.na(Longitude))
# saveRDS(polling.SA, file="polling.SA.rds")
polling.SA <- readRDS("polling.SA.rds")
# results.SA <- read.csv("HouseStateFirstPrefsByPollingPlaceDownload-20499-SA.csv", stringsAsFactors = FALSE) %>% filter(StateAb == "SA")
# results.SA$PartyAb[results.SA$PartyAb == ""] <- "INF"
# saveRDS(results.SA, file="results.SA.rds")
results.SA <- readRDS("results.SA.rds")
election.SA <- merge(results.SA, polling.SA)

# polling.NSW <- read.csv("GeneralPollingPlacesDownload-20499.csv", stringsAsFactors = FALSE) %>% filter(State == "NSW", !is.na(Latitude), !is.na(Longitude))
# saveRDS(polling.NSW, file="polling.NSW.rds")
polling.NSW <- readRDS("polling.NSW.rds")
# results.NSW <- read.csv("HouseStateFirstPrefsByPollingPlaceDownload-20499-NSW.csv", stringsAsFactors = FALSE, skip = 1) %>% filter(StateAb == "NSW")
# results.NSW$PartyAb[results.NSW$PartyAb == ""] <- "INF"
# saveRDS(results.NSW, file="results.NSW.rds")
results.NSW <- readRDS("results.NSW.rds")
election.NSW <- merge(results.NSW, polling.NSW)

election.SA %<>% group_by(PollingPlaceID) %>% arrange(-OrdinaryVotes) %>% mutate(PPwinner = first(PartyAb), PPvotes = sum(OrdinaryVotes), Ncandidates = n_distinct(CandidateID)) %>% filter(PPvotes > 0)
election.SA %<>% group_by(DivisionID) %>% arrange(-OrdinaryVotes) %>% mutate(ELECwinner = first(PartyAb), ELECvotes = sum(OrdinaryVotes))
election.SA.CT <- SharedData$new(election.SA)

AEC.SA@data <- merge(AEC.SA@data, election.SA %>% group_by(DivisionNm) %>% summarise(ELECwinner = unique(ELECwinner)), by.x = "Elect_div", by.y = "DivisionNm")

election.NSW %<>% group_by(PollingPlaceID) %>% arrange(-OrdinaryVotes) %>% mutate(PPwinner = first(PartyAb), PPvotes = sum(OrdinaryVotes)) %>% filter(PPvotes > 0)
election.NSW %<>% group_by(DivisionID) %>% arrange(-OrdinaryVotes) %>% mutate(ELECwinner = first(PartyAb), ELECvotes = sum(OrdinaryVotes))
election.NSW.CT <- SharedData$new(election.NSW)

AEC.NSW@data <- merge(AEC.NSW@data, election.NSW %>% group_by(DivisionNm) %>% summarise(ELECwinner = unique(ELECwinner)), by.x = "Elect_div", by.y = "DivisionNm")

partycols <- c("red","blue","yellow","green","#FF9900","purple","grey","white")
partylabs <- c("ALP","LP","NP","GRN","XEN","AJP","IND","INF")

```

SA
=====================================  

Column 
-----------------------------------------------------------------------

### 

```{r fig.height=2.6, fig.width=12}

# c("#0000FF", "#FF0000", "#FF9900", "#00CC00")

plot_ly(election.SA.CT, type = "scatter", x = DivisionNm, y = Swing, text = paste("Total Votes = ", PPvotes),
  mode = "markers", color = PPwinner, colors = c("#0000FF", "#FF0000", "#FF9900")) %>%
  layout(dragmode = "select")

```

Column {.tabset .tabset-fade} 
-----------------------------------------------------------------------

### Votes by Polling Places

```{r fig.height=2.6, fig.width=12}

# c("#0000FF", "#FF0000", "#FF9900", "#00CC00")

plot_ly(election.SA.CT, type = "scatter", x = PollingPlaceNm, y = OrdinaryVotes, color = PPwinner, colors = c("#0000FF", "#FF0000", "#FF9900"), mode = "markers") %>%
  layout(dragmode = "select", yaxis = list(title="Votes"))

```

### Overall Votes

```{r fig.height=2.6, fig.width=12}

# c("#0000FF", "#FF0000", "#FF9900", "#00CC00")

plot_ly(election.SA.CT, type = "bar", x = DivisionNm, y = PPvotes, color = PPwinner, colors = c("#0000FF", "#FF0000", "#FF9900")) %>%
  layout(dragmode = "select", yaxis = list(title="Votes"), barmode="stack")


```
















Column
-----------------------------------------------------------------------


### 

```{r fig.height=7, fig.width=8}

Adelaide <- structure(c(34.929, 138.600972222222), .Names = c("lat", "lon"))

factpal <- colorFactor(partycols , factor(AEC.SA@data$ELECwinner, levels = partylabs))

map <- leaflet(election.SA.CT) %>% addProviderTiles("CartoDB.Positron", options = providerTileOptions(opacity = 0.6)) %>%
    addPolygons(data = AEC.SA, fillColor = ~factpal(ELECwinner), color = "grey", weight = 3) %>% 
    setView(lng = Adelaide[2], lat = -Adelaide[1], zoom = 11) %>%
    addLegend(colors = partycols, labels = partylabs) %>%
    addCircleMarkers(lat = ~Latitude, lng = ~Longitude, popup = ~PollingPlaceNm, color = ~factpal(PPwinner), radius = 6, stroke = FALSE, fillOpacity = 0.9)

map
```

NSW {data-orientation=columns}
===================================== 

Column
-----------------------------------------------------------------------













Column {.tabset .tabset-fade} 
-----------------------------------------------------------------------

### Votes by Polling Places

```{r fig.height=2.6, fig.width=12}

# c("#0000FF", "#FF0000", "#FF9900", "#00CC00")

plot_ly(election.NSW.CT, type = "scatter", x = PollingPlaceNm, y = OrdinaryVotes, color = PPwinner, colors = c("#FFFF00","#0000FF", "#FF0000", "#00CC00", "#CCCCCC"), mode = "markers") %>%
  layout(dragmode = "select", yaxis = list(title="Votes"))

```

### Overall Votes

```{r fig.height=2.6, fig.width=12}

# c("#0000FF", "#FF0000", "#FF9900", "#00CC00")

plot_ly(election.NSW.CT, type = "bar", x = DivisionNm, y = PPvotes, color = PPwinner, colors = c("#FFFF00","#0000FF", "#FF0000", "#00CC00", "#CCCCCC")) %>%
  layout(dragmode = "select", yaxis = list(title="Votes"), barmode="stack")


```















Column
-----------------------------------------------------------------------

### 

```{r fig.height = 1, fig.width=2}
filter_slider("swingSelect", "Select range of swing to filter by", election.SA.CT, ~Swing, step = 0.1)
```

### 

```{r fig.height=7, fig.width=8}

Sydney <- structure(c(33.8688, 151.2093), .Names = c("lat", "lon"))

factpal <- colorFactor(partycols , factor(AEC.NSW@data$ELECwinner, levels = partylabs))

map <- leaflet(election.NSW.CT) %>% addProviderTiles("CartoDB.Positron", options = providerTileOptions(opacity = 0.6)) %>%
    addPolygons(data = AEC.NSW, fillColor = ~factpal(ELECwinner), color = "grey", weight = 3) %>% 
    setView(lng = Sydney[2], lat = -Sydney[1], zoom = 11) %>%
    addLegend(colors = partycols, labels = partylabs) %>%
    addCircleMarkers(lat = ~Latitude, lng = ~Longitude, popup = ~PollingPlaceNm, color = ~factpal(PPwinner), radius = 6, stroke = FALSE, fillOpacity = 0.9)

map
```